package com.hr.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hr.pojo.Department;
import com.hr.pojo.Position;
import com.hr.service.DepartmentService;
import com.hr.service.impl.DepartmentServiceImpl;


public class DepartmentServlet  extends BaseServlet {
	private DepartmentService departmentService=new DepartmentServiceImpl();
	
    public String listDepartment(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");  
		List departmentList= departmentService.findAllDepartments();
		request.setAttribute("departmentList", departmentList);
		request.getRequestDispatcher("/admin/listdepartment.jsp").include(request, response);
    	return null; 
    }
    public String addDepartment(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");  
		request.getRequestDispatcher("/admin/adddepartment.jsp").include(request, response);
    	return null; 
    }
    public String toUpdatePage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	Department department =new Department();
    	department.setName(request.getParameter("name"));
    	department.setId(new Long(request.getParameter("id")));
    	departmentService.updateDepartment(department);
    	response.setContentType("text/html;charset=utf-8");  
    	request.getRequestDispatcher("./public/alert.jsp").include(request, response);
        PrintWriter out = response.getWriter();
        out.println("<script>ok_alert(\"修改成功!\",\"main.jsp?method=listDepartment&servlet=DepartmentServlet\");</script>");
    	return null;
    }
    public String UpdateDepartment(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String  id = request.getParameter("id");	
    	response.setContentType("text/html;charset=utf-8");  
    	Department departmentList= departmentService.findDepartmentById(id);
		request.setAttribute("departmentList", departmentList);
    	response.setContentType("text/html;charset=utf-8");  
		request.getRequestDispatcher("/admin/updatedepartment.jsp").include(request, response);
    	return null; 
    }
    
    public String addToDepartment(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	Department department =new Department();
    	department.setName(request.getParameter("name"));
    	departmentService.saveDepartment(department);
    	response.setContentType("text/html;charset=utf-8");  
    	request.getRequestDispatcher("./public/alert.jsp").include(request, response);
        PrintWriter out = response.getWriter();
        out.println("<script>ok_alert(\"添加成功!\",\"main.jsp?method=listDepartment&servlet=DepartmentServlet\");</script>");
    	return null;    	
    }
	public String departmentDelete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		departmentService.delete(id);
		response.setContentType("text/html;charset=utf-8");  
		request.getRequestDispatcher("./public/alert.jsp").include(request, response);
        PrintWriter out = response.getWriter();
        out.println("<script>ok_alert(\"删除成功\",\"main.jsp?method=listDepartment&servlet=DepartmentServlet\");</script>");
		return null;
	}
}
